package com.example.innovate.service.impl;

import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.AdminInfoDOExample;
import com.example.innovate.mapper.AdminInfoDOMapper;
import com.example.innovate.service.AdminService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminInfoDOMapper adminInfoDOMapper;

    @Override
    public List<AdminInfoDO> findAllAdmin(int pageNumber, int pageSize) {
        AdminInfoDOExample adminInfoDOExample = new AdminInfoDOExample();
        PageHelper.startPage(pageNumber,pageSize);
        return adminInfoDOMapper.selectByExample(adminInfoDOExample);
    }

    @Override
    public AdminInfoDO getAdminInfoByName(String username) {
        AdminInfoDOExample adminInfoDOExample = new AdminInfoDOExample();
        adminInfoDOExample.createCriteria()
                .andAdminUserNameEqualTo(username);
        List<AdminInfoDO> adminInfoDO = adminInfoDOMapper.selectByExample(adminInfoDOExample);
        if(!adminInfoDO.isEmpty()){
            return adminInfoDO.get(0);
        }
        return null;
    }

    @Override
    public AdminInfoDO getAdminInfoById(int id) {
        AdminInfoDOExample adminInfoDOExample = new AdminInfoDOExample();
        adminInfoDOExample.createCriteria()
                .andAdminIdEqualTo(id);
        List<AdminInfoDO> adminInfoDO = adminInfoDOMapper.selectByExample(adminInfoDOExample);
        if(!adminInfoDO.isEmpty()){
            return adminInfoDO.get(0);
        }
        return null;
    }

    @Override
    public void addAdminInfo(AdminInfoDO adminInfoDO) {
        adminInfoDOMapper.insertSelective(adminInfoDO);
    }

    @Override
    public void deleteAdminInfo(int id) {
        adminInfoDOMapper.deleteByPrimaryKey(id);
    }

    @Override
    public AdminInfoDO updateAdminInfo(AdminInfoDO adminInfoDO) {
        adminInfoDOMapper.updateByPrimaryKeySelective(adminInfoDO);
        return null;
    }
}
